//
//  NSString+MD5.m
//  CarDeal_IOS
//
//  Created by 孙可 on 2018/8/21.
//  Copyright © 2018年 sunke. All rights reserved.
//

#import "NSString+MD5.h"
#import "CommonCrypto/CommonDigest.h"

@implementation NSString (MD5)

- (NSString *)md5
{
    const char *cStr = [self UTF8String];
    unsigned char result[16];
    CC_MD5(cStr, (CC_LONG)strlen(cStr), result);
    return [NSString stringWithFormat:
            @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
            result[0], result[1], result[2], result[3],
            result[4], result[5], result[6], result[7],
            result[8], result[9], result[10], result[11],
            result[12], result[13], result[14], result[15]
            ];
}

+(NSString *)formatPhone:(NSString *)number{
    //首先验证是不是手机号码
    
    NSString *MOBILE = @"^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|7[06-8])\\\\d{8}$";
    
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    
    BOOL isOk = [regextestmobile evaluateWithObject:number];
    
    if (isOk) {//如果是手机号码的话
        
        NSString *numberString = [number stringByReplacingCharactersInRange:NSMakeRange(3, 4) withString:@"****"];
        
        return numberString;
        
    }
    
    return number;
}

+ (NSInteger)getTextWidth:(NSString *)str andFont:(UIFont *)font{
   
    NSDictionary *dic = @{NSFontAttributeName:font};  //指定字号
    CGRect rect = [str boundingRectWithSize:CGSizeMake(0, 30)/*计算宽度时要确定高度*/ options:NSStringDrawingUsesLineFragmentOrigin |
                   NSStringDrawingUsesFontLeading attributes:dic context:nil];
    return rect.size.width;
}

+(NSInteger)getTextHeight:(NSString *)str andFont:(UIFont *)font{
    NSDictionary *dic = @{NSFontAttributeName:font};  //指定字号
    CGRect rect = [str boundingRectWithSize:CGSizeMake(0, 30)/*计算宽度时要确定高度*/ options:NSStringDrawingUsesLineFragmentOrigin |
                   NSStringDrawingUsesFontLeading attributes:dic context:nil];
    return rect.size.height;
}


+ (int)compareStr1:(NSString *)str1 withStr2:(NSString *)str2{
    long time1 = [self getNumByTimestr:str1];
    long time2 = [self getNumByTimestr:str2];
    if(time1 - time2>0){
        return 1;
    }else if(time1 == time2){
        return 0;
    }else{
        return -1;
    }
}

+(long)getNumByTimestr:(NSString *) str{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSDate * date = [formatter dateFromString:str];
    return [date timeIntervalSince1970];
}

@end
